# coding: utf-8
import unittest
import Info
from mock import MagicMock, ANY
from test.Helpers import PseudoMachine

class VMInfoTests(unittest.TestCase):
  
    def test_should_call_getArray_to_list_all_machines(self):
        #given
        mgrgetArrayMock = MagicMock()
        vminfo = Info.VMInfo()
        vminfo.mgr.getArray = mgrgetArrayMock
        
        #when
        vminfo.listMachineNames()
        
        #then
        mgrgetArrayMock.assert_called_once_with(ANY, 'machines')
        
    def test_should_return_name_and_id_of_VMs(self):
        #given
        expected = [(u'mach1', u'12345')]
        toBeReturned = [PseudoMachine("mach1", "12345")]
        vminfo = Info.VMInfo()
        vminfo.mgr.getArray = MagicMock(return_value=toBeReturned)
       
        #when
        machs = vminfo.listMachineNames()
        
        #then
        self.assertEqual(expected, machs)
        
    def test_should_return_all_VMs(self):
        #given
        toBeReturned = [PseudoMachine("mach1", "12345")]
        vminfo = Info.VMInfo()
        vminfo.mgr.getArray = MagicMock(return_value=toBeReturned)
       
        #when
        machs = vminfo.getAllMachines()
        
        #then
        self.assertEqual(toBeReturned, machs)
        
    def test_should_find_mach_by_name(self):
        #given
        machName="mach1"
        expected = PseudoMachine(machName, "12345")
        toBeReturned = [expected]
        vminfo = Info.VMInfo()
        vminfo.getAllMachines = MagicMock(return_value=toBeReturned)
       
        #when
        mach = vminfo.findMachine(machName)
        
        #then
        self.assertEqual(expected, mach)

    def test_should_find_mach_by_id(self):
        #given
        machId="12345"
        expected = PseudoMachine("mach1", machId)
        toBeReturned = [expected]
        vminfo = Info.VMInfo()
        vminfo.getAllMachines = MagicMock(return_value=toBeReturned)
       
        #when
        mach = vminfo.findMachine(machId)
        
        #then
        self.assertEqual(expected, mach)
        
    def test_should_return_none_when_machine_was_not_founded(self):
        #given
        machId="12345"
        toBeReturned = [PseudoMachine("mach1", "987654")]
        vminfo = Info.VMInfo()
        getAllMachinesMock = MagicMock(return_value=toBeReturned)
        vminfo.getAllMachines = getAllMachinesMock
        
        #when
        mach = vminfo.findMachine(machId)
        
        #then
        self.assertEqual(None, mach)
        getAllMachinesMock.assert_called_once_with()

def main():
    unittest.main()

if __name__ == '__main__':
    main()
    
